package com.qlslylq.ad.sdk.core.mgr;

import com.qlslylq.ad.sdk.model.rsp.AdPlaceWebConfigureVO;
import com.qlslylq.ad.sdk.model.rsp.AdPlaceWebVO;
import com.qlslylq.ad.sdk.util.SPUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * created by qlslylq <br/>
 * on 2022/2/11 <br/>
 * 广告位策略管理类<br/>
 */
public class PolicyMgr {

    private static PolicyMgr instance;

    private AdPlaceWebVO splashPolicy;
    private AdPlaceWebVO rewardedPolicy;
    private AdPlaceWebVO fullScreenPolicy;
    private AdPlaceWebVO interstitialPolicy;
    private AdPlaceWebVO bannerPolicy;
    private AdPlaceWebVO feedPolicy;

    private PolicyMgr() {

    }

    public static PolicyMgr getInstance() {
        if (instance == null) {
            instance = new PolicyMgr();
        }
        return instance;
    }

    /**
     * 获取当前策略<br/>
     */
    public AdPlaceWebVO getPolicy(String policyKey) {
        if (SPUtils.JO_SPLASH_POLICY.equals(policyKey)) {
            return splashPolicy;
        } else if (SPUtils.JO_REWARD_POLICY.equals(policyKey)) {
            return rewardedPolicy;
        } else if (SPUtils.JO_FULL_SCREEN_POLICY.equals(policyKey)) {
            return fullScreenPolicy;
        } else if (SPUtils.JO_INTERSTITIAL_POLICY.equals(policyKey)) {
            return interstitialPolicy;
        } else if (SPUtils.JO_BANNER_POLICY.equals(policyKey)) {
            return bannerPolicy;
        } else if (SPUtils.JO_FEED_POLICY.equals(policyKey)) {
            return feedPolicy;
        }
        return null;
    }

    /**
     * 获取策略配置-组量<br/>
     */
    public int getPolicyGroupSize(String policyKey) {
        AdPlaceWebVO item = getPolicy(policyKey);
        if (item != null) {
            return item.getConfigures().size();
        }
        return 0;
    }

    /**
     * 获取策略配置-组量<br/>
     */
    public int getPolicyGroupSize(AdPlaceWebVO item) {
        if (item != null) {
            return item.getConfigures().size();
        }
        return 0;
    }

    /**
     * 获取策略配置-组<br/>
     */
    public List<AdPlaceWebConfigureVO> getPolicyGroup(String policyKey, int cursor) {
        AdPlaceWebVO item = getPolicy(policyKey);
        if (item != null) {
            if (!item.getConfigures().isEmpty() && cursor >= 0 && cursor < item.getConfigures().size()) {
                return item.getConfigures().get(cursor);
            }
        }
        return new ArrayList<>();
    }

    /**
     * 设置当前策略<br/>
     */
    public void setPolicy(String policyKey, AdPlaceWebVO jo) {
        if (SPUtils.JO_SPLASH_POLICY.equals(policyKey)) {
            splashPolicy = jo;
        } else if (SPUtils.JO_REWARD_POLICY.equals(policyKey)) {
            rewardedPolicy = jo;
        } else if (SPUtils.JO_FULL_SCREEN_POLICY.equals(policyKey)) {
            fullScreenPolicy = jo;
        } else if (SPUtils.JO_INTERSTITIAL_POLICY.equals(policyKey)) {
            interstitialPolicy = jo;
        } else if (SPUtils.JO_BANNER_POLICY.equals(policyKey)) {
            bannerPolicy = jo;
        } else if (SPUtils.JO_FEED_POLICY.equals(policyKey)) {
            feedPolicy = jo;
        }
    }

    /**
     * 重置策略<br/>
     */
    public void resetPolicy(String policyKey) {
        //不再重置，继续保存
    }

}
